package LeetCode.month04.day120;

import java.util.*;

/**
 * @author 16778
 * @date 2025/11/30 8:13
 * @description
 */public class No1590 {
    public int minSubarray(int[] nums, int p) {
        int n = nums.length;
        int[] pre = new int[n+1];
        for (int i = 1; i <= n; i++) {
            pre[i] = (pre[i-1]+nums[i-1])%p;
        }
        int x = pre[n];
        if(x == 0){
            return 0;
        }
        int min = n;
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            map.put(pre[i],i);
            int j = map.getOrDefault((pre[i+1]-x+p)%p, -n);
            min = Math.min(min,i-j);
        }
        return min == n ? -1 : min;
    }
}
